package com.jy.api.controller.web;

import com.jy.api.service.NoticeSrvice;
import com.jy.api.signUpReq.web.WebNoticeSignUpReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by lihu on 2018/3/3.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/web", produces = "application/json;charset=utf-8")
public class WebNoticeCotroller {


    @Autowired
    NoticeSrvice noticeSrvice;


    /**
     * 呼叫同步
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/notice")
    public ResponseEntity addNotice(@Valid @RequestBody WebNoticeSignUpReq signUpReq,
                                    BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【呼叫失败】参数不正确, signUpReq={}", signUpReq);
            return new ResponseEntity(bindingResult.getFieldError().getDefaultMessage(), HttpStatus.BAD_REQUEST);
        }
        try {
            noticeSrvice.addNotice(signUpReq);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            log.error("addNotice is error signUpReq:{}", signUpReq, ex);
            return new ResponseEntity("系统运行错误！", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}
